package com.stox.shared.indicator.model;

import com.stox.shared.indicator.model.config.BaseIndicatorConfig;
import com.stox.shared.indicator.model.enumeration.IndicatorDisplayType;
import com.stox.shared.indicator.model.enumeration.IndicatorPosition;

public class WeightedMovingAverage  extends AbstractIndicator<Double, BaseIndicatorConfig>{

	public static final WeightedMovingAverage INSTANCE = new WeightedMovingAverage();
	
	private WeightedMovingAverage() {
		super("WMA");
	}

	@Override
	public IndicatorDisplayType getDisplayType(final BaseIndicatorConfig config) {
		return IndicatorPosition.INDEPENDENT.equals(getPosition(config)) ? IndicatorDisplayType.AREA : IndicatorDisplayType.LINE;
	}

	@Override
	public IndicatorPosition getPosition(final BaseIndicatorConfig config) {
		switch (config.getBarValue()) {
			case VOLUME:
				return IndicatorPosition.OVERLAY_VOLUME;
			case RANGE:
				return IndicatorPosition.INDEPENDENT;
			default:
				return IndicatorPosition.OVERLAY_PRICE;
		}
	}

	@Override
	public Double getValue(final BaseIndicatorConfig configuration, final Double[] in, final int index) throws UnsupportedOperationException {
		final int span = configuration.getSpan();
		if(in.length <= index + span){
			return null;
		}
		double sum = 0;
		double denominator = 0;
		for (int i = index; i < (index + span); i++) {
			denominator += index + span - i;
			sum += in[i]*(index + span - i);
		}
		return sum/denominator;
	}
	
	@Override
	public Double[] getValues(BaseIndicatorConfig configuration, Double[] in) throws UnsupportedOperationException {
		Double[] result = new Double[in.length];
		for(int index = 0; index < in.length; index++){
			result[index] = getValue(configuration, in, index);
		}
		return result;
	}
	
	@Override
	public BaseIndicatorConfig createDefaultConfiguration() {
		return new BaseIndicatorConfig();
	}
}
